#!/bin/python

import socket
import random

def firstplayer_setup(args):
    packetSize = 32
    HOST = '' #localhost
    PORT = random.randint(49125, 65535)
    
    print "FIRST PLAYER"
    
    localsoc = socket.socket(AF_NET, SOCK_STREAM)
    try:
        localsoc.bind((HOST, PORT)) # binds the server to the local address
    except socket.error: 
        print "Internal Socket error: retrying..."
        try:
            PORT = random.randint(49125, 65535)
            localsoc.bind((HOST, PORT)) # binds the server to the local address
                                        # random port
        except socket.error:
            print "Internal Socket error: retrying..."
            try:
                PORT = random.randint(49125, 65535)
                localsoc.bind((HOST, PORT))     # binds the server to the local 
                                                # random port
            except socket.error:
                print "Internal Socket failed: exiting"
                return1
    
    
    
    localsoc.shutdown(SHUT-RDWR) # shuts down the socket in a timely fashion
    localsoc.close()             # closes the socket completely
    return 0